package com.huatai.bi.vo;

import cn.hutool.core.text.csv.*;
import cn.hutool.core.util.CharsetUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: itc-airport-cloud
 * @BelongsPackage: com.huatai.bi.vo
 * @Author: hecaiy
 * @CreateTime: 2024-06-12  20:00
 * @Description: TODO
 * @Version: 1.0
 */
public class test {

    /**
     * 读取csv中的数据
     * @param file
     * @return
     * @throws IOException
     */
    public static List<Map<String,Object>> csvImport(File file) throws IOException {
        //2. 进行配置
        CsvReadConfig csvReadConfig=new CsvReadConfig();
        // 是否跳过空白行
        csvReadConfig.setSkipEmptyRows(true);
        // 是否设置首行为标题行
        csvReadConfig.setContainsHeader(true);
        //构建 CsvReader 对象
        CsvReader csvReader = CsvUtil.getReader(csvReadConfig);
        // 这里转了下 可能会产生临时文件，临时文件目录可以设置，也可以立马删除
//        File file = multipartFile2File(multipartFile);
        CsvData read = csvReader.read(file, CharsetUtil.CHARSET_GBK);
        List<Map<String,Object>> mapList = new ArrayList<>();
        List<String> header = read.getHeader();
        List<CsvRow> rows = read.getRows();
        for (CsvRow row : rows) {
            Map<String,Object> map = new HashMap<>();
            for (int i = 0; i < row.size(); i++) {
                map.put(header.get(i),row.get(i));
            }
            mapList.add(map);
        }
        return mapList;
    }

    /**
     * multipartFile转File
     **/
    public static File multipartFile2File(MultipartFile multipartFile){
        File file = null;
        if (multipartFile != null){
            try {
                file=File.createTempFile("tmp", null);
                multipartFile.transferTo(file);
                System.gc();
                file.deleteOnExit();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return file;
    }
    public static void main(String[] args) throws IOException {
        List<Map<String, Object>> list = csvImport(new File("D:\\aaa.csv"));
        String aaa="";
//        String sdjhkh = PinyinUtil.getPinyin("sdfsdfsdfsd","");
//        String hsdjfhjksdhf = PinyinUtil.getFirstLetter("sdfsdfsdfsd","");
//        FileInputStream fileInputStream = new FileInputStream("D:\\aaa.xls");
//        //使用hutool工具读取数据
//        ExcelReader reader = ExcelUtil.getReader(fileInputStream);
//        List<String> sheetNames = reader.getSheetNames();
//        List<Map<String, Object>> maps = reader.read(0,0,100);
//        System.out.println(JSON.toJSONString(maps));


//        EasyExcel.read(fileInputStream, LinkedHashMap.class, new DemoDataListener(list -> {
//            for (int i = 0; i < list.size(); i++) {
//                System.out.println(JSON.toJSONString(list.get(i)));
//            }
//        }) {}).doReadAll();
    }

//    public static void main(String[] args) throws FileNotFoundException {
//
//
//
//
//        //获取excel sheet清单
//        ExcelReader excelReader = EasyExcel.read(new FileInputStream("D:\\aaa.xlsx")).build();
//        List<ReadSheet> readSheets = excelReader.excelExecutor().sheetList();
//        //获取csv sheet清单
//        List<Map<String, String>> excelContent = EasyExcels.getExcelContent(new FileInputStream("D:\\aaa.csv"), LinkedHashMap.class);
//        String aaa = "";
//
//        List<Map<Integer, String>> list = null;
//        FileInputStream fileInputStream = new FileInputStream("D:\\aaa.csv");
//        try {
//            list = EasyExcel.read(fileInputStream)
//                    .excelType(ExcelTypeEnum.CSV)
//                    .sheet()
//                    .headRowNumber(1)
//                    .doReadSync();
//        } catch (Exception e) {
//            throw new RuntimeException("读取 Excel 文件失败");
//        }
//        StringBuilder stringBuilder = new StringBuilder();
//        for (int i=0;i<list.size();i++) {
//            // 转为 LinkedHashMap 主要是为了保证读取的数据和表格顺序一致
//            LinkedHashMap<Integer, String> linkedHashMap = (LinkedHashMap) list.get(i);
//            for (Map.Entry<Integer, String> entry : linkedHashMap.entrySet()) {
//                String value = entry.getValue();
//                Integer key = entry.getKey();
//                System.out.println(key + ":" + value);
//            }
//        }
//        System.out.println(stringBuilder.toString());
//    }
}
